package com.joeyzh.odata;

import android.net.Uri;

import java.util.HashMap;

/**
 * Created by Joey on 2018/12/18.
 *
 * @author by Joey
 *         OData url
 */

public class ODataUri {


    private String url;

    public String getUrl() {
        return url;
    }

    public static class Builder {

        private final String url;

        private HashMap<String, String> propertysMap = new HashMap<>();

        public Builder(String url) {
            this.url = url;
        }

        public Builder addOperation(ODataType type, String condition) {
            propertysMap.put(type.toString(), Uri.encode(condition));
            return this;
        }

        public ODataUri build() {
            ODataUri uri = new ODataUri();
            if (propertysMap.isEmpty()) {
                uri.url = url;
                return uri;
            }
            StringBuffer tmp = new StringBuffer(url);
            tmp.append("?");
            for (String property : propertysMap.keySet()) {
                tmp.append(property);
                tmp.append("=");
                tmp.append(propertysMap.get(property));
                tmp.append("&");
            }
            uri.url = tmp.substring(0, tmp.length() - 1);
            return uri;
        }
    }
}
